#include "qtextwidget.h"
#include "ui_qtextwidget.h"

#include <QColorDialog>

using namespace CORELIB;

QTextWidget::QTextWidget(QWidget *parent) :
    QAbstractStylesheetWidget(parent),
    ui(new Ui::QTextWidget)
{
    ui->setupUi(this);
}

QTextWidget::~QTextWidget()
{
    delete ui;
}


QIcon QTextWidget::createIcon(const QGradient &gradient)
{
    QPixmap pixmap=QPixmap(16,16);
    pixmap.fill(gradient.stops().first().second);
    QIcon icon=QIcon(pixmap);
    icon.addPixmap(pixmap,QIcon::Disabled);
    return icon;
}

void QTextWidget::setItem(const tagStylesheetItem &item)
{
    QGradient g;

    g=item.m_attributes.value("color").value<QGradient>();
    if(g.type()==QGradient::NoGradient)
    {
        g.setColorAt(0,QColor(0,0,0,0));
    }
    ui->pushButton->setIcon(createIcon(g));
    QString str;
    QColor c=g.stops().first().second;
    str=str.sprintf("#%02X%02X%02X%02X",c.alpha(),c.red(),c.green(),c.blue());
    ui->pushButton->setText(str);

    QAbstractStylesheetWidget::setItem(item);
}

void CORELIB::QTextWidget::on_pushButton_clicked()
{
    QColor c=QColorDialog::getColor(Qt::white,this);
    if(c.isValid())
    {
        QGradient g=QLinearGradient(0,0,1,0);
        g.setColorAt(0,c);
        g.setColorAt(1,c);
        ui->pushButton->setIcon(createIcon(g));
        QString str;
        QColor c=g.stops().first().second;
        str=str.sprintf("#%02X%02X%02X%02X",c.alpha(),c.red(),c.green(),c.blue());
        ui->pushButton->setText(str);

        QVariant v;
        v.setValue<QGradient>(g);
        m_item.m_attributes.insert("color",v);
        emit stylesheetChanged();
    }
}

void QTextWidget::takeResource()
{

}
